package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.DiscountTypeEnum;
import com.epfly.promo.activity.dao.ActivityFoodMapper;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.ActivityFoodEntity;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 菜品过滤
 */
@Order(value = PredicateOrder.FOOD_ORDER)
@Service
public class FoodPredicate implements ActivityDiscountPredicate {

    @Resource
    private ActivityFoodMapper activityFoodMapper;

    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity activityEntity = activityWrapper.getActivityEntity();
        // 优惠方式为菜品折扣
        if (activityEntity.getDiscountType().equals(DiscountTypeEnum.FOOD_REBATE.getKey())) {

            List<String> foodIdList = activityWrapper.getFoodIdList();

            ActivityFoodEntity entity = activityFoodMapper.existsByActivityIdAndFoodIdList(activityEntity.getId(), foodIdList);
            // 活动关联菜品中未找到用户下单的菜品
            if (Objects.isNull(entity)) {
                return false;
            }
        }

        return true;
    }
}
